package com.shiwaixiangcun.customer.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.TextView;

import com.shiwaixiangcun.customer.R;


/**
 * Author   Administrator
 * Date     2018/1/25
 * Desc     带箭头的TextView
 * @author Administrator
 */

public class ArrowTextView extends TextView {
    private int color;

    public ArrowTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        ini(context, attrs);
    }


    public ArrowTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        ini(context, attrs);
    }


    public ArrowTextView(Context context) {
        super(context);
    }

    private void ini(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArrowTextView);

        color = typedArray.getColor(R.styleable.ArrowTextView_bg, Color.WHITE);
    }

    /**
     * @param color 箭头矩形的背景色.........
     */
    public void setBgColor(int color) {
        this.color = color;
        invalidate();

    }

    @Override
    protected void onDraw(Canvas canvas) {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color == 0 ? Color.RED : color);
        float height = getHeight();
        float width = getWidth();
        Path path = new Path();
        float triangle = getPaddingLeft() / 2;
        path.moveTo(0, height / 2);
        path.lineTo(triangle, 0);
        path.lineTo(width, 0);
        path.lineTo(width, height);
        path.lineTo(triangle, height);
        path.lineTo(0, height / 2);
        canvas.drawPath(path, paint);
        super.onDraw(canvas);

    }


}